package com.haopeng.common.exception;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.haopeng.common.response.ResponseModel;
/**
 * 全局异常处理类
 * @author Administrator
 *
 */
@ControllerAdvice
public class ControllerExceptionAdvice {

	private static final Logger Logger=LoggerFactory.getLogger(ControllerExceptionAdvice.class);
	/**
	 *    全局系统异常处理
	 * @param ex
	 * @return
	 */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseModel systemErrorHandler(Exception ex) {
    	Logger.error("系统异常", ex);
        return ResponseModel.failure(ex.getMessage());
    }
    
    /**
	 *    业务异常处理
	 * @param ex
	 * @return
	 */
    @ResponseBody
    @ExceptionHandler(value = CommonException.class)
    public ResponseModel commonErrorHandler(CommonException ex) {
    	Logger.error("业务异常code:{}", ex.getCode(),ex);
        return new ResponseModel(ex.getCode(), ex.getMessage());
    }
    
    
}
